<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Model\Activity;
use App\Http\Controllers\Controller;

class ActivityController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $activitys = Activity::orderBy('date_time', 'DESC')->get();

        return view('admin.activity.list',['activitys' => $activitys]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.activity.form');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
                'title' => 'required',
                'img' => 'required',
                'desc' => 'required',
                'date_time' => 'required',
            ]);

        $file = $request->file('img');
        $file_name = $file->getClientOriginalName();

        if (!$file->move('activitys/', $file_name)) {

            return view('admin.activity.form');
        }
        $activity = new Activity();

        $activity->title = $request->input('title');
        $activity->img = 'activitys/' . $file_name;
        $activity->desc = $request->input('desc');
        $activity->date_time = $request->input('date_time');


        if ($activity->save()) {

            return redirect('admin/activitys');
        }else{

            return view('admin.activity.form', $activity);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $activity = Activity::find($id);
        return view('admin.activity.form', $activity);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $activity = Activity::find($id);

        $this->validate($request, [
                'title' => 'required',
                //'img' => 'required',
                'desc' => 'required',
                'date_time' => 'required',
            ]);

        $file_name = '';

        if ($request->file('img')) {
            
            $file = $request->file('img');
            $file_name = $file->getClientOriginalName();

            if (!$file->move('activitys/', $file_name)) {

                return view('admin.activity.form');
            }
        }
        

        $activity->title = $request->input('title');
        $activity->img = $file_name ? ('activitys/' . $file_name) : $activity->img;
        $activity->desc = $request->input('desc');
        $activity->date_time = $request->input('date_time');


        if ($activity->save()) {

            return redirect('admin/activitys');
        }else{

            return view('admin.activity.form', $activity);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Activity::destroy($id);
        return redirect('/admin/activitys');
    }
}
